package org.fixies.spreadsheet.converter;

import org.fixies.spreadsheet.Cell;
import org.fixies.spreadsheet.Row;
import org.fixies.spreadsheet.Unit;

import java.util.*;


public class ListMapConverter extends
        AbstractDataSetConverter<List<Map<String, Cell>>> {


    @Override
    public List<Map<String, Cell>> convert() {
        Unit unit = getDataSet();
        // 結果
        List<Map<String, Cell>> result =
                new ArrayList<Map<String, Cell>>(unit.size());

        Iterator<Row> itr = unit.iterator();
        if (!itr.hasNext()) {
            return result;
        }

        // 一行目は見出し（キー）
        for (Row midashi = itr.next(); itr.hasNext();) {
            Row data = itr.next();
            Map<String, Cell> m = new HashMap<String, Cell>(data.size());
            for (int i = 0; i < midashi.size(); i++) {
                String key = midashi.get(i).asString();
                Cell value = data.get(i);
                m.put(key, value);
            }
            result.add(m);
        }
        return result;
    }
}
